package nu.board.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import nu.board.model.BoardReplyListVO;
import nu.board.model.BoardVO;
import nu.board.model.CategoryVO;
import nu.board.model.ConfirmVO;
import nu.board.model.GroupVO;
import nu.board.model.MemberGroupVO;
import nu.board.model.PlanVO;
import nu.board.model.ReplyVO;
import nu.board.service.BoardService;
import nu.member.model.MemberVO;
import nu.member.service.MemberService;
import nu.paging.ListVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BoardController {
	
	@Resource(name="boardService")
	private BoardService boardService;
	@Resource(name="memberService")
	private MemberService memberService;
	
	/**
	 * 그룹의 리스트를 보여주는 메소드
	 */
	@RequestMapping("showGroupList.nu")
	public ModelAndView showGroupList(HttpSession session,String category) throws Exception{
		//System.out.println("category check"+category);
		List<GroupVO> lvo = boardService.showGroupList(category);
		
		for(int i=0;i<lvo.size();i++){
			String photo = memberService.getPhotoById(lvo.get(i).getId());
			lvo.get(i).setPhoto(photo);
		}
		
		return new ModelAndView("board/"+category+"_list","lvo",lvo);
	}
	
	/**
	 * 특정 그룹에서 쓴 글의 목록을 보여주는 메소드
	 */
	@RequestMapping("showPageList.nu")
	public ModelAndView showPageList(HttpSession session, CategoryVO cvo) throws Exception{
		//System.out.println(cvo);
		ListVO listvo = boardService.getPostingList(cvo);
		return new ModelAndView("board/board_list","listvo",listvo);
	}
	
	/**
	 * 새로운 글을 쓰기 위한 페이지를 보여주는 메소드
	 */
	@RequestMapping("showWriteBoard.nu")
	public ModelAndView showWriteBoard() throws Exception{
		return new ModelAndView("board/board_register");
	}
	
	/**
	 * 작성한 글을 DB에 입력시키는 메소드
	 */
	@RequestMapping("writeContent.nu")
	public ModelAndView writeContent(BoardVO bvo) throws Exception{
		System.out.println(bvo);
		boardService.writeContent(bvo);
		return new ModelAndView("redirect:/showContentHits.nu?no="+bvo.getNo());
	}
	
	/**
	 * 글의 내용을 가져오는 메소드
	 */
	@RequestMapping("showContentHits.nu")
	public ModelAndView showContentHits(int no) throws Exception{
		BoardVO bvo = boardService.getContent(no);
		System.out.println(bvo);
		List replyList=boardService.getReplyListByBoardNo(no);
		BoardReplyListVO brlist=new BoardReplyListVO(bvo,replyList);
		return new ModelAndView("board/board_content","brlist",brlist);
	}
	
	/**
	 * 댓글 입력 메소드
	 */
	@RequestMapping("registerReply.nu")
	public ModelAndView registerReply(ReplyVO rvo) throws Exception{
		int result=boardService.registerReply(rvo);
		//System.out.println(result);
		return new ModelAndView("redirect:/showContentHits.nu?no="+rvo.getNo());
	}
	
	/**
	 * 댓글 리스트 가져오는 메소드
	 */
	@RequestMapping("getReplyListByBoardNo.nu")
	public List getReplyListByBoardNo(int no) throws Exception{
		//System.out.println(no);
		List list=boardService.getReplyListByBoardNo(no);
		//System.out.println(list);
		return list;
	}
	
	/**
	 * 그룹 만드는 페이지로 이동시키는 것
	 */
	@RequestMapping("showCreateGroup.nu")
	public ModelAndView showCreateGroup() throws Exception{
		return new ModelAndView("board/group_register");
	}
	
	/**
	 * 그룹 등록하기
	 */
	@RequestMapping("registerGroup.nu")
	public ModelAndView registerGroup(HttpSession session, GroupVO gvo) throws Exception{
		System.out.println(gvo);
		boardService.registerGroup(gvo); //그룹을 생성시켜주고
		// 그룹을 생성한사람은 그 그룹에 바로 가입시키게 처리하고...
		// 그룹과 생성 성공과 동시에 Session에 있는 Member의 상태 변경
		MemberGroupVO mgvo= new MemberGroupVO(0, gvo.getId(), gvo.getGroup_type(), gvo.getGroup_id());
		if(session!=null){		//session 다시 set
			if(session.getAttribute("login")!=null){
				boolean flag=true;
				MemberVO mvo = (MemberVO)session.getAttribute("login");
				List<GroupVO> list = memberService.getGroupListByMember(mvo);
				for(int i=0;i<list.size();i++){
					if(list.get(i).getGroup_type().equals(mgvo.getGroup_type())){
						flag=false;
						break;
					}
				}
				if(flag){
					int result=memberService.joinGroup(mgvo);
					List<GroupVO> list2 = memberService.getGroupListByMember(mvo);
					mvo.setList(list2);
					session.setAttribute("login", mvo);
				}
			}
		}
		return new ModelAndView("redirect:/showGroupList.nu?category="+gvo.getGroup_type());
	}
	@RequestMapping("confirm.nu")
	public ModelAndView confirmBoard(String writer,ConfirmVO cfvo,String category) throws Exception{
		System.out.println(writer+cfvo+category);
		//해당 bvo의 confirmation +1하기
		//bvo는 nu_confirmed 요기 insert하는 용도...
		boardService.confirmedBoard(cfvo);
		//category에 따라 writer의 포인트 올려주는 용도...
		if(category.equals("study")){
			memberService.increaseStudyPoint(writer);
		}else if(category.equals("sports")){
			memberService.increaseSportsPoint(writer);
		}else if(category.equals("smoking")){
			memberService.increaseSmokingPoint(writer);
		}else if(category.equals("finance")){
			memberService.increaseFinancePoint(writer);
		}else if(category.equals("living")){
			memberService.increaseLivingPoint(writer);
		}
		//category에 따라 
		
		return new ModelAndView("redirect:/showContentHits.nu?no="+cfvo.getNo());
	}
	/**
	 * 그룹의 plan 글쓰는 곳으로 이동
	 */
	@RequestMapping("insertPlanPage.nu")
	public ModelAndView insertPlanPage(GroupVO gvo) throws Exception{
		System.out.println(gvo);
		return new ModelAndView("plan/insertPlanPage");
	}
	/**
	 * 그룹의 plan 쓰기
	 */
	@RequestMapping("insertPlan.nu")
	public ModelAndView insertPlan(PlanVO pvo ) throws Exception{
		System.out.println("parameter"+pvo);
		boardService.insertPlan(pvo);
		System.out.println("result"+pvo);
		return new ModelAndView("redirect:/showPageList.nu?group_type="+pvo.getGroup_type()+"&group_id="+pvo.getGroup_id()+"&masterid="+pvo.getId()+"&pageNo=1");
	}
	@RequestMapping("showPlanDetail.nu")
	public ModelAndView showPlanDetail(int nu_plan_no) throws Exception{
		PlanVO pvo=boardService.showPlanDetail(nu_plan_no);
		System.out.println(pvo);
		return new ModelAndView("plan/showPlanDetail","pvo",pvo);
	}
}
